package fu.capstone.hanoicityguide.common.vgservice;

import fu.capstone.hanoicityguide.R;
import fu.capstone.hanoicityguide.presentation.activity.MainActivity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.location.LocationManager;
import android.util.Log;

public class ProximityIntentReceiver extends BroadcastReceiver {

	private Intent browsePlaceIntent;
	private PendingIntent pendingIntent;

	@Override
	public void onReceive(Context context, Intent intent) {

		String key = LocationManager.KEY_PROXIMITY_ENTERING;

		// Intent i = new Intent(context, ProxAlertActivity.class);
		// PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
		// intent, 0);

		boolean entering = intent.getBooleanExtra(key, false);

		if (entering) {
			Log.d(getClass().getSimpleName(), "entering "
					+ intent.getBundleExtra("PlaceInfo").getString("PlaceName"));

			NotificationManager notificationManager = (NotificationManager) context
					.getSystemService(Context.NOTIFICATION_SERVICE);

			String placeName = intent.getBundleExtra("PlaceInfo").getString(
					"PlaceName");

			// Notification notification = createNotification();
			// notification.setLatestEventInfo(context, "Proximity Alert!",
			// "You are near: " + placeName, pendingIntent);

			browsePlaceIntent = new Intent(context, MainActivity.class);
			// browsePlaceIntent.setAction(Long.toString(System.currentTimeMillis()));
			browsePlaceIntent.setFlags(
					Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
			browsePlaceIntent.putExtra("PlaceID",
					intent.getBundleExtra("PlaceInfo").getInt("PlaceID"));
			Log.v("Receiver place ID", ""+ intent.getBundleExtra("PlaceInfo").getInt("PlaceID"));
			Log.v("Receiver place request code id",""+ intent.getBundleExtra("PlaceInfo").getInt("RequestCodeID"));
			pendingIntent = PendingIntent.getActivity(context, intent
					.getBundleExtra("PlaceInfo").getInt("RequestCodeID"),
					// intent.getBundleExtra("PlaceInfo").getInt("PlaceID"),
					browsePlaceIntent, PendingIntent.FLAG_CANCEL_CURRENT);
			
			Notification not = new Notification.Builder(context)
					.setAutoCancel(true)
					.setContentText("You have came near by " + placeName)
					.setContentTitle("Voice guidance")
					.setSmallIcon(R.drawable.ic_menu_notifications)
					.setLights(Color.WHITE, 1500, 1500)
					// .setDefaults(Notification.DEFAULT_LIGHTS)
					.setDefaults(Notification.DEFAULT_VIBRATE)
					.setWhen(System.currentTimeMillis())
					.setContentIntent(pendingIntent).build();
			notificationManager.notify(intent.getBundleExtra("PlaceInfo")
					.getInt("RequestCodeID"), not);
		} else {
			Log.d(getClass().getSimpleName(), "exiting "
					+ intent.getBundleExtra("PlaceInfo").getString("PlaceName"));
		}

	}
}
